#/bin/bash
# git的地址https://github.com/coturn/coturn
#####################################
# 配置文件:cp ${INSTALL_DIR}/turn/etc/turnserver.conf.default ${INSTALL_DIR}/turn/etc/turnserver.conf
# 具体的配置文件
# listening-port=3478  #端口要打开
# external-ip=xxx.xxx.xxx.xxx  #服务器的ip地址
# user=yll:lili  #设置用户名和密码
# realm=turn.example.cn  #解析到服务器ip的域名
# cli-password=admin@1234 #启动服务的密码
# lt-cred-mech
### 配置的https的证书
# cert=/usr/local/etc/cert.pem
# pkey=/usr/local/etc/key.pem
# ${INSTALL_DIR}/turn/bin/turnserver  -c  ${INSTALL_DIR}/turn/etc/turnserver.conf -o  #启动,后面的'-o'是以守护进程启动
# 将3478添加到防火墙中
## firewall-cmd --zone=public --add-port=3478/udp --permanent
## firewall-cmd --zone=public --add-port=3478/tcp --permanent
# 测试地址:https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
# 查看端口netstat  -nltp
# 在window上去ping是否可用telnet turn.example.com  3478
#####################################

#下载地址
RESOURCE_URL="https://github.com/coturn/coturn/archive/refs/tags/4.5.1.3.tar.gz"
#解压后的文件名
RESOURCE_FOLDER="4.5.1.3"
DOWNLOAD_DIR="/home/tools"
INSTALL_DIR="/usr/local"
LOG_FILE="${DOWNLOAD_DIR}/installcoturn.log"
#安装依赖工具
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
    yum -y install make wget
fi
rpm -q openssl-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum -y install make openssl-devel
fi
rpm -q libevent2 &>/dev/null
if [ $? -ne 0 ]; then
    yum -y install make libevent2
fi
rpm -q libevent-devel &>/dev/null
if [ $? -ne 0 ]; then
    yum -y install make libevent-devel
fi

#如果源文件不存在则下载
if [ ! -d "${DOWNLOAD_DIR}" ]; then
    mkdir -p ${DOWNLOAD_DIR}
fi

cd ${DOWNLOAD_DIR}

if [ ! -f "${RESOURCE_FOLDER}.tar.gz" ]; then
    echo $(date +"%Y-%m-%d %H:%M:%S") ":开始下载资源文件;" >>${LOG_FILE}
    wget -c $RESOURCE_URL
fi
#如果源文件存在，
if [ -f "${RESOURCE_FOLDER}.tar.gz" ]; then
    #则检测是否已解压
    if [ ! -d "${RESOURCE_FOLDER}" ]; then
        tar -zxvf ${RESOURCE_FOLDER}.tar.gz
    fi
fi
if [ -d "coturn-${RESOURCE_FOLDER}" ]; then
    cd ${DOWNLOAD_DIR}/"coturn-${RESOURCE_FOLDER}"
    ./configure --prefix=${INSTALL_DIR}/turn
    if [ $? -eq 0 ]; then
        make && make install
        if [ $? -eq 0 ]; then
            echo $(date +"%Y-%m-%d %H:%M:%S")":${RESOURCE_FOLDER} 安装完成" >>${LOG_FILE}
        else
            echo $(date +"%Y-%m-%d %H:%M:%S")":${RESOURCE_FOLDER} 安装失败" 2>>${LOG_FILE}
        fi
    fi
fi
